package cn.itsource;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/*
* 该类是生产者和消费者的配置类
* */
@Configuration
public class RabbitmqConfig {
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    //定义交换机
    @Bean(EXCHANGE_TOPICS_INFORM)//bean的名字
    public Exchange exchangeTopicsInform() {
        //durable(true)持久化，消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //定义邮箱队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue queueInformEmail(){
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    //定义短信队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue queueInformSms(){
        return new Queue(QUEUE_INFORM_SMS);
    }

    //绑定短信队列到交换机,调用方法方式
    @Bean
    public Binding bindingQueueInformSms() {
        return BindingBuilder.bind(queueInformSms()).to(exchangeTopicsInform())
                .with("sms.#").noargs();
    }

    @Bean//@Qualifier(QUEUE_INFORM_SMS)指定队列
    public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("email.#").noargs();
    }
}


